package vn.com.atm;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.com.atm.database.UserFunctions;
import vn.com.atm.map.CustomItemizedOverlay;
import vn.com.atm.map.CustomOverlayItem;
import vn.com.atm.map.RoutePathOverlay;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MapViewActivity extends MapActivity{
    MapView mapView;
	private MapController mapController;
	private GeoPoint geoPoint;
	List<Overlay> mapOverlays;
	Drawable drawable, drawable2;
	RoutePathOverlay rp;
	UserFunctions us;
	JSONObject js;
	
	CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay, itemizedOverlay1;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        
        mapView = (MapView)findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        mapOverlays = mapView.getOverlays();
        
       int mAtm = R.drawable.atm1;
       Bitmap bm = BitmapFactory.decodeResource(getResources(),mAtm);
     // first overlay
     		drawable = getResources().getDrawable(R.drawable.bigflag);
     		
     		
     		itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);
     	
        
        AtmDetail atm = new AtmDetail();
        Bundle bundle = getIntent().getExtras();
		double strLatDes = Double.parseDouble(bundle.getString("Lat"));
		double strLongDes = Double.parseDouble(bundle.getString("Long"));
		double strLatSrc = Double.parseDouble(atm.getMyLat());
		double strLongSrc = Double.parseDouble(atm.getMyLong());
        
        
        UserFunctions us = new UserFunctions();
     	
		JSONObject jsonObject = new JSONObject();
        jsonObject = us.getPoints(strLatSrc, strLongSrc, strLatDes, strLongDes);
     	JSONArray routeArray = null;
		try {
			routeArray = jsonObject.getJSONArray("routes");
		} catch (JSONException e) {
			e.printStackTrace();
		}
     	JSONObject routes = null;
     	JSONArray legs = null;
		try {
			routes = routeArray.getJSONObject(0);
			legs = routes.getJSONArray("legs");
		} catch (JSONException e) {
			e.printStackTrace();
		}
     	JSONObject overviewPolylines = null;
     	JSONObject line = null;
     	String addressStart = "";
     	String addressEnd = "";
     	
		try {
			overviewPolylines = routes.getJSONObject("overview_polyline");
			line = legs.getJSONObject(0);
			addressStart = line.getString("start_address");
			addressEnd = line.getString("end_address");
			
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
     	String encodedString = null;
		try {
			encodedString = overviewPolylines.getString("points");
		} catch (JSONException e) {
			e.printStackTrace();
		}
     	List<GeoPoint> pointToDraw = decodePoly(encodedString);
     	mapView.getOverlays().add(new RoutePathOverlay(pointToDraw));
        BitmapDrawable bd = (BitmapDrawable)drawable;
        int width = bd.getBitmap().getWidth();
        int height = bd.getBitmap().getHeight();
        
       
        geoPoint = new GeoPoint((int)(strLatSrc*1E6)-width/2, (int)(strLongSrc*1E6)-height/2);
        CustomOverlayItem overlayItem = new CustomOverlayItem(geoPoint, "Điểm xuất phát", 
				"(Địa chỉ: "+ addressStart, 
				bm);
        
        itemizedOverlay.addOverlay(overlayItem);
        mapOverlays.add(itemizedOverlay);
       
		drawable2 = getResources().getDrawable(R.drawable.bigflag);
		itemizedOverlay1 = new CustomItemizedOverlay<CustomOverlayItem>(drawable2, mapView);
		
		
	    
		GeoPoint point3 = new GeoPoint((int)(strLatDes*1E6)-width/2,(int)(strLongDes*1E6)-height/2);
		CustomOverlayItem overlayItem3 = new CustomOverlayItem(point3, "Điểm đích", 
				"Địa chỉ:" +  addressEnd, bm);
		itemizedOverlay1.addOverlay(overlayItem3);
	
		
		mapOverlays.add(itemizedOverlay1);
       
     	

      
  
        
        
		mapController = mapView.getController();
		mapController.setZoom(16);
		mapController.animateTo(geoPoint);
		
	}
	private List<GeoPoint> decodePoly(String encoded) {

        List<GeoPoint> poly = new ArrayList<GeoPoint>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;

        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;

            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6), (int) (((double) lng / 1E5) * 1E6));
            poly.add(p);
        }

        return poly;
    }
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	

}
